<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="btn">发送Ajax请求</button>
    <script>
        var btn = document.querySelector('#btn');
        btn.onclick = function(){
            //创建Ajax对象 
        var xhr = new XMLHttpRequest();
        //告诉Ajax请求方式和请求地址
        xhr.open('get','http://localhost:3000/error');
        //3.发送请求
        xhr.send();
        //获取服务器端响应的数据
        xhr.onload = function(){
            // xhr.responseText响应文本
            console.log(xhr.responseText);
            console.log(xhr.status);
            if(xhr.status == 400){
                alert('请求出错');
            }
        }
        //当网络中断时会触发onerror事件
        xhr.onerror = function(){
            alert('网络中断，无法发送Ajax请求')
        }
        }
    </script>
</body>
</html>